/** 
 * 2011-10-10 下午02:22:59 
 * TcpServer.java 
 * author:sunb 
 */
package com.u6.tcp;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.lamfire.utils.Bytes;

/**
 * @author sunbin
 * 
 */
public class TcpServer {

	public static void main(String[] args) {
		try {
			ServerSocket serverSocket = new ServerSocket(8989);

			while (true) {
				Socket socket = serverSocket.accept();

				// TODO Auto-generated method stub
				InputStream inputStream;

				inputStream = socket.getInputStream();

				OutputStream outputStream = socket.getOutputStream();
				byte[] bs = new byte[4];
				inputStream.read(bs);
				int length = Bytes.toInt(bs);


				byte[] stringBs = new byte[length];
				inputStream.read(stringBs);
				String s = new String(stringBs);
				System.out.println("recice:"+s);

				if (s.equals("over")) {
					sendMessage(outputStream);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void sendMessage(OutputStream outputStream) {
		try {
			String sendMesaage = "服务器发送消息";
			byte[] bs = sendMesaage.getBytes();
			outputStream.write(bs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
